<?php

require_once 'config.php';

require_once 'includes/functions.php';

// 检查登录状态

if (!isLoggedIn()) {

    redirect('login.php');

}

$apps = getApps();

$editingApp = null;

// 检查是否在编辑模式

if (isset($_GET['edit'])) {

    $editingApp = getAppById($_GET['edit']);

}

?>

<?php include 'includes/header.php'; ?>

<h2 style="margin: 20px 0;">后台管理</h2>

<!-- 添加/编辑应用表单 -->

<div style="background: white; padding: 20px; border-radius: 10px; margin-bottom: 30px;">

    <h3><?php echo $editingApp ? '编辑应用' : '添加新应用'; ?></h3>

    

    <form action="action/<?php echo $editingApp ? 'edit_app.php' : 'add_app.php'; ?>" method="POST">

        <?php if ($editingApp): ?>

            <input type="hidden" name="id" value="<?php echo $editingApp['id']; ?>">

        <?php endif; ?>

        

        <div class="form-group">

            <label for="name">应用名称:</label>

            <input type="text" id="name" name="name" class="form-control" 

                   value="<?php echo $editingApp ? htmlspecialchars($editingApp['name']) : ''; ?>" required>

        </div>

        

        <div class="form-group">

            <label for="icon">图标直链:</label>

            <input type="url" id="icon" name="icon" class="form-control" 

                   value="<?php echo $editingApp ? htmlspecialchars($editingApp['icon']) : ''; ?>" required>

        </div>

        

        <div class="form-group">

            <label for="download_url">下载链接:</label>

            <input type="url" id="download_url" name="download_url" class="form-control" 

                   value="<?php echo $editingApp ? htmlspecialchars($editingApp['download_url']) : ''; ?>" required>

        </div>

        

        <div class="form-group">

            <label for="version">版本号:</label>

            <input type="text" id="version" name="version" class="form-control" 

                   value="<?php echo $editingApp ? htmlspecialchars($editingApp['version']) : ''; ?>" required>

        </div>

        

        <div class="form-group">

            <label for="description">应用介绍:</label>

            <textarea id="description" name="description" class="form-control" required><?php echo $editingApp ? htmlspecialchars($editingApp['description']) : ''; ?></textarea>

        </div>

        

        <div class="form-group">

            <label for="update_content">更新内容:</label>

            <textarea id="update_content" name="update_content" class="form-control" required><?php echo $editingApp ? htmlspecialchars($editingApp['update_content']) : ''; ?></textarea>

        </div>

        

        <button type="submit" class="btn"><?php echo $editingApp ? '更新应用' : '添加应用'; ?></button>

        

        <?php if ($editingApp): ?>

            <a href="admin.php" class="btn btn-secondary">取消编辑</a>

        <?php endif; ?>

    </form>

</div>

<!-- 应用管理列表 -->

<h3>应用管理</h3>

<?php if (empty($apps)): ?>

    <div style="text-align: center; padding: 20px; background: white; border-radius: 10px;">

        <p>暂无应用</p>

    </div>

<?php else: ?>

    <div style="background: white; border-radius: 10px; overflow: hidden;">

        <table style="width: 100%; border-collapse: collapse;">

            <thead>

                <tr style="background: #f8f9fa;">

                    <th style="padding: 15px; text-align: left; border-bottom: 1px solid #dee2e6;">应用名称</th>

                    <th style="padding: 15px; text-align: left; border-bottom: 1px solid #dee2e6;">版本</th>

                    <th style="padding: 15px; text-align: left; border-bottom: 1px solid #dee2e6;">操作</th>

                </tr>

            </thead>

            <tbody>

                <?php foreach ($apps as $app): ?>

                    <tr>

                        <td style="padding: 15px; border-bottom: 1px solid #dee2e6;">

                            <strong><?php echo htmlspecialchars($app['name']); ?></strong>

                        </td>

                        <td style="padding: 15px; border-bottom: 1px solid #dee2e6;">

                            <?php echo htmlspecialchars($app['version']); ?>

                        </td>

                        <td style="padding: 15px; border-bottom: 1px solid #dee2e6;">

                            <a href="admin.php?edit=<?php echo $app['id']; ?>" class="btn btn-secondary" style="padding: 5px 10px; font-size: 12px;">编辑</a>

                            <form action="action/delete_app.php" method="POST" style="display: inline;">

                                <input type="hidden" name="id" value="<?php echo $app['id']; ?>">

                                <button type="submit" class="btn btn-danger" style="padding: 5px 10px; font-size: 12px;" onclick="return confirm('确定删除这个应用吗？')">删除</button>

                            </form>

                        </td>

                    </tr>

                <?php endforeach; ?>

            </tbody>

        </table>

    </div>

<?php endif; ?>

<?php include 'includes/footer.php'; ?>